<?php
namespace api\modules\shop\controllers;

use common\models\Shop;
use yii\web\Controller;
use Yii;
use common\service\ConstantService;
use yii\base\Module;
use yii\web\Response;

class ShopBaseController extends Controller
{
    /**
     * 加密所用到的盐,请勿修改,上线时在改
     * @var string
     */
    protected  $salt = 'a321df354sa35d1f';

    /**
     * 所记录的cookie名,请修改成项目名.
     * @var string
     */
    protected  $auth_cookie = "api";

    protected $page_size = 10;

    protected $shop = null;

    protected $allow_actions = [
        'shop/shop/auto-login',
        'shop/shop/send-code',
        'shop/shop/save-shop-info'

    ];

    public function __construct($id, Module $module, array $config = [])
    {
        parent::__construct($id, $module, $config);
    }

    /**
     * 这里要初始化权限
     * @param \yii\base\Action $action
     * @return bool
     */
    public function beforeAction($action)
    {
        if(in_array($action->getUniqueId(),$this->allow_actions))
        {
            return true;
        }
//        $token = $this->get('token');
        $token = isset(Yii::$app->request->headers['Auth']) ? Yii::$app->request->headers['Auth']:'';
        if($token){
            $data = explode('-',base64_decode($token));
            $openid = base64_decode($data[0]);
            $shop = Shop::find()->where(['openid'=>$openid])->asArray()->one();
            if($shop){
                $this->shop = $shop;
                if(empty($shop['shop_logo'])){
                    $shop_model = Shop::findOne(['id'=>$shop['id']]);
                    $shop_model->shop_logo = 'white149837668.jpg';
                    $shop_model->save();
                }
                return true;
            }
        }
        return $this->renderJson([],'该用户不存在,请重新获取',ConstantService::$response_fail);

        //以后也许需要用户强制授权
    }


    /**
     * 判断是否为ajax请求.
     * @return bool|mixed
     */
    public function isAjax()
    {
        return Yii::$app->request->isAjax;
    }

    /**
     * 判断是否为post请求.
     * @return bool|mixed
     */
    public function isPost()
    {
        return Yii::$app->request->isPost;
    }

    /**
     * 判断是否为Get请求.
     * @return bool|mixed
     */
    public function isGet()
    {
        return Yii::$app->request->isGet;
    }

    /**
     * 获取queryString参数
     * @param string $key
     * @param string $default
     * @return array|string
     */
    public function get($key = '',$default = '')
    {
        $query = Yii::$app->request->getQueryParams();

        if(isset($query[$key]))
        {
            return $query[$key];
        }

        if(!$key)
        {
            return $query;
        }

        return $default;
    }

    /**
     * 获取post提交参数
     * @param string $key
     * @param string $default
     * @return array|string
     */
    public function post($key = '',$default = '')
    {
        $query = Yii::$app->request->getBodyParams();

        if(isset($query[$key]))
        {
            return $query[$key];
        }

        if(!$key)
        {
            return $query;
        }

        return $default;
    }

    /**
     * 渲染json请求.
     * @param array $data
     * @param string $msg
     * @param int $code
     */
    public function renderJson($data = [],$msg = '',$code = -1)
    {
        // 设置响应头信息.
        Yii::$app->response->format = Response::FORMAT_JSON;

        Yii::$app->response->data = [
            'data'  =>  $data,
            'msg'   =>  $msg,
            'code'  =>  $code,
            'req_id'=>  $this->geneReqId()
        ];

        return Yii::$app->end();
    }

    /**
     * 获取唯一值.
     * @return string
     */
    private function geneReqId()
    {
        return uniqid();
    }

}